clear;
origin=imread('C:/Code/Matlab/HW1_data/building.jpg');
origin=single(rgb2gray(origin));
% min_value=min(min(origin));
% max_value=max(max(origin));
peak_thresh=0.3;
edge_thresh=3;

[f,d]=vl_sift(origin,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
size_origin=size(f);
C.Location=f(1:2,:)';
%3(a) plot image and features
% figure;
% imshow(origin,[0,255]);
% hold on;
% vl_plotframe(f);

%3(b) brightness
% ratio_offset=[];
% for offset=-100:20:100
%     offset_img=origin+offset;
%     [f_o,d_o]=vl_sift(offset_img,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
%     match=vl_ubcmatch(d,d_o,2);
%     size_offset=size(match);
%     ratio_offset=[ratio_offset,size_offset(2)/size_origin(2)];
% end 
% figure;
% plot(-100:20:100,ratio_offset);

%3(c) contrast
% ratio_contrast=[];
% origin_0_1=origin/255;
% for contrast=0.5:0.25:2
%     change_img=imadjust(origin_0_1,[0,1],[0,1],contrast);
%     change_img=single(change_img*255);
%     [f_o,d_o]=vl_sift(change_img,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
%     match=vl_ubcmatch(d,d_o,2);
%     size_offset=size(match);
%     ratio_contrast=[ratio_contrast,size_offset(2)/size_origin(2)];
% end 
% figure;
% plot(0.5:0.25:2,ratio_contrast);

%3(d) gaussian noise
% ratio_gauss_noise=[];
% for deviation=0:5:30
%     primary_noise=randn(size(origin));
%     gauss_noise=primary_noise*deviation;
%     change_img=origin+gauss_noise;
%     [f_o,d_o]=vl_sift(change_img,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
%     match=vl_ubcmatch(d,d_o,2);
%     size_offset=size(match);
%     ratio_gauss_noise=[ratio_gauss_noise,size_offset(2)/size_origin(2)];
% end 
% figure;
% plot(0:5:30,ratio_gauss_noise);

%3(e) gaussian kernel
ratio_gauss_noise=[];
for deviation=1:1:10
    H = fspecial('gaussian',[10*deviation+1,10*deviation+1],deviation);
    change_img=imfilter(origin,H,'replicate');
    [f_o,d_o]=vl_sift(change_img,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
    match=vl_ubcmatch(d,d_o,2);
    size_offset=size(match);
    ratio_gauss_noise=[ratio_gauss_noise,size_offset(2)/size_origin(2)];
end 
figure;
plot(1:1:10,ratio_gauss_noise);

% ratio_array=[];
% for angle=0:15:360
%     rotate_i=imrotate(cover_1,angle);
%     [f_r,d_r]=vl_sift(rotate_i,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
%     C_rotate.Location=f_r(1:2,:)';
%     ratio=MatchPoint(C,C_rotate,size(cover_1),angle);
%     ratio_array=[ratio_array,ratio];
%     fprintf('%d : %8.5f\n',angle,ratio);
% end
% createfigure(0:15:360,ratio_array,'SIFT keypoint detector robustness against rotation');
% ratio_array2=[];
% scale_array=[];
% for size_ratio=0:1:8
%     scale_size=1.2^size_ratio;
%     scale_array=[scale_array,scale_size];
%     resize_img=imresize(cover_1,scale_size,'bicubic');
%     [f_s,d_s]=vl_sift(resize_img,'PeakThresh', peak_thresh,'edgethresh', edge_thresh);
%     C_resize.Location=f_s(1:2,:)';
%     ratio=MatchPoint2(C,C_resize,scale_size);
%     ratio_array2=[ratio_array2,ratio];
% end
% createfigure2(scale_array,ratio_array2,'SIFT keypoint detector robustness against scale');